package com.example.lib_cache.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_cache.R;
import com.example.lib_cache.dao.OnEndPoiNameClickListener;
import com.example.lib_cache.dao.OnPoiNameClickListener;
import com.example.lib_cache.databinding.ItemSelectdesBinding;
import com.example.lib_cache.databinding.ItemSelectendBinding;
import com.example.lib_cache.model.EndPoi;
import com.example.lib_common.util.LiveDataManager;

import java.util.List;

public class EndPoiAdapter extends RecyclerView.Adapter<EndPoiAdapter.EndPoiViewHoder>{
    private List<EndPoi> itemDatas;

    private Context context;

    public EndPoiAdapter(List<EndPoi> itemDatas) {
        this.itemDatas = itemDatas;
    }

    public EndPoiAdapter(){}

    @NonNull
    @Override
    public EndPoiAdapter.EndPoiViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSelectendBinding itemSelectendBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_selectend,
                parent,
                false);
        this.context = parent.getContext();

        return new EndPoiAdapter.EndPoiViewHoder(itemSelectendBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull EndPoiAdapter.EndPoiViewHoder holder, final int position) {
        holder.itemSelectendBinding.setPoiEnd(itemDatas.get(position));
        holder.itemSelectendBinding.executePendingBindings();
        holder.itemSelectendBinding.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("选中了", String.valueOf(position));
                MutableLiveData<EndPoi> poiName = LiveDataManager
                        .getInstance().with("EndPoiInfo", EndPoi.class);
                poiName.setValue(itemDatas.get(position));
                if(onEndPoiNameClickListener!=null) {
                    onEndPoiNameClickListener.finishThisActivity();
                }

            }
        });
    }

    public void updateData(List<EndPoi> poiList) {
        if (null == poiList) {
            return;
        }
        itemDatas = poiList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null == itemDatas ? 0 : itemDatas.size();
    }

    class EndPoiViewHoder extends RecyclerView.ViewHolder{

        ItemSelectendBinding itemSelectendBinding;


        public EndPoiViewHoder(ItemSelectendBinding itemSelectendBinding) {
            super(itemSelectendBinding.getRoot());
            this.itemSelectendBinding = itemSelectendBinding;

        }
    }

    private OnEndPoiNameClickListener onEndPoiNameClickListener;

    public void setOnEndPoiNameClickListener(OnEndPoiNameClickListener listener){
        this.onEndPoiNameClickListener = listener;
    }
}
